package com.neuedu.mblog.service;

import com.neuedu.mblog.dao.CommentDao;
import com.neuedu.mblog.dao.FavoriteDao;
import com.neuedu.mblog.entiry.Comment;
import com.neuedu.mblog.entiry.Favorite;
import com.neuedu.mblog.entiry.User;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目：mblog_java4
 * 创建时间：  2021-12-29   9:21
 * 作者 :jshand
 * 描述 :
 */
public class FavoriteService {
    private FavoriteDao favoriteDao = new FavoriteDao();





    public boolean deleteById(HttpServletRequest request) throws SQLException {
        String id = request.getParameter("id");
        return favoriteDao.delete(Integer.parseInt(id))>0;
    }



    public void pageList(HttpServletRequest request) throws SQLException, ClassNotFoundException {
        Integer pageNum = 1;
        Integer pageSize = 10;


        String strPageNum = request.getParameter("pageNum");
        String strPageSize = request.getParameter("pageSize");
        if(strPageNum !=null && !"".equals(strPageNum)){
            pageNum =  Integer.parseInt(strPageNum);
        }
        if(strPageSize !=null && !"".equals(strPageSize)){
            pageSize =  Integer.parseInt(strPageSize);
        }


        //当前登录的用户
        User user = (User) request.getSession().getAttribute("user");

        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT 										");
        sql.append("   mto_favorite.id id,                          ");
        sql.append("    mto_favorite.user_id user_id,               ");
        sql.append("   mto_post.id post_id,                         ");
        sql.append("   mto_post.`title` ,                           ");
        sql.append("   mto_favorite.`created`                       ");
        sql.append(" FROM                                           ");
        sql.append("   mto_favorite                                 ");
        sql.append("   INNER JOIN mto_post                          ");
        sql.append("     ON mto_favorite.`post_id` = mto_post.`id`  ");
        sql.append("     AND user_id = ?                            ");



        ArrayList paramList = new ArrayList();
        paramList.add(user.getId());

        //select count(1) from (sql)
        long total = favoriteDao.total(sql.toString() ,paramList.toArray());
        long pageCount = total%pageSize == 0?total/pageSize:(total/pageSize)+1;

        sql .append( " limit  ? ,?");
        paramList.add((pageNum-1) * pageSize);
        paramList.add(pageSize);

        List<Favorite> pageList = favoriteDao.selectList(sql.toString(),paramList.toArray());

        //使用request共享数据
        request.setAttribute("list",pageList);
        request.setAttribute("total",total);
        request.setAttribute("pageNum",pageNum);
        request.setAttribute("pageSize",pageSize);
        request.setAttribute("pageCount",pageCount);

//        System.out.println("total = " + total);
//        System.out.println("pageCount = " + pageCount);
//        System.out.println("pageSize = " + pageSize);
//        System.out.println("pageNum = " + pageNum);
//        System.out.println("list = " + pageList);


    }
}
